<?php

namespace App\Service\GuildManage;

use App\Constants\ErrorCode;
use App\Exception\ApiException;
use App\Model\Guild\GuildAccountDaily;
use App\Model\Guild\GuildHost;
use App\Model\Guild\GuildManage;
use App\Model\GuildManage\GuildAccount;
use App\Model\GuildManage\GuildBalance;
use App\Service\BaseService;
use App\Service\ServiceTrait\GuildTrait;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Di\Annotation\Inject;
class PanelService extends BaseService
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct(\Hyperf\AsyncQueue\Driver\DriverFactory $driverFactory)
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        $this->__handlePropertyHandler(__CLASS__);
    }
    use GuildTrait;
    /**
     * 用户id
     * @var
     */
    private $actionUserId;
    /**
     * 返回数组
     * @var
     */
    private $return;
    /**
     * @Inject
     * @var GuildBalance
     */
    protected $guildBalanceModel;
    /**
     * @Inject
     * @var GuildAccount
     */
    protected $guildAccountModel;
    /**
     * @Inject
     * @var GuildAccountDaily
     */
    protected $guildAccountDailyModel;
    /**
     * @Inject
     * @var GuildManage
     */
    protected $guildManageModel;
    /**
     * @Inject
     * @var GuildHost
     */
    protected $guildHostModel;
    /**
     * 推广员数据
     * @var Builder|Model|object|null
     */
    private $manageInfo;
    /**
     * 主控制器
     * @return mixed
     */
    public function handle()
    {
        $this->actionUserId = $this->getUserId();
        if (!$this->checkGuildStatus($this->actionUserId)) {
            throw new ApiException('亲,暂时没有此操作权限哦!');
        }
        $this->resetReturn();
        $this->manageInfo = $this->guildManageModel->getGuildInfoByUserId($this->actionUserId);
        $this->return['guild_name'] = $this->manageInfo->name;
        $this->getBalance();
        $this->getAccount();
        $this->getShareLink();
        $this->getEarnings();
        $this->getMemberData();
        return $this->return;
    }
    /**
     * 获取余额数据
     * @return void
     */
    private function getBalance()
    {
        $balance = $this->guildBalanceModel->getBalanceInfoByUserId($this->actionUserId);
        if (!empty($balance)) {
            $this->return['total_balance'] = bcadd($balance->have_access_balance, $balance->audit_access_balance, 2);
            $this->return['have_access_balance'] = '可提现余额：' . $balance->have_access_balance;
            $this->return['audit_access_balance'] = '审核中金额：' . $balance->audit_access_balance;
        }
    }
    /**
     * 是否有提现账号
     * @return void
     */
    private function getAccount()
    {
        if ($this->guildAccountModel->getCountByUserId($this->actionUserId) > 0) {
            $this->return['is_have_account'] = 1;
        }
    }
    /**
     * 获取分享链接
     * @return void
     */
    private function getShareLink()
    {
        $this->return['share_url'] = env('BIND_LINK', 'http://web.xtiaowa.com/xinmi/guild_bind.html') . '?guild_id=' . $this->manageInfo->guild_id;
    }
    /**
     * 获取收益数据
     * @return void
     */
    private function getEarnings()
    {
        $data = $this->guildAccountDailyModel->where('guild_manage_id', $this->manageInfo->id)->orderBy('id', 'desc')->first();
        if (empty($data)) {
            $data->dt = date("Y-m-d", strtotime("-1 day"));
            $data->voice_income = 0;
            $data->video_income = 0;
            $data->msg_income = 0;
            $data->gift_income = 0;
            $data->invite_recharge_income = 0;
            $data->share_ratio = 0;
        }
        $baseIncome = $data->voice_income + $data->video_income + $data->msg_income + $data->gift_income;
        $inviteIncome = $data->invite_recharge_income;
        $share = $data->share_ratio;
        $this->return['total_earnings'] = (string) bcdiv(bcadd(bcmul($baseIncome, $share, 2), bcmul($inviteIncome, $share, 2), 2), 10, 2);
        $this->return['earnings_date'] = '日期:' . $data->dt;
        $earningsList = ['msg_income', 'video_income', 'voice_income', 'gift_income'];
        foreach ($earningsList as $one) {
            $text = '';
            $value = (string) bcdiv(bcmul($data->{$one}, $share, 2), 10, 2);
            switch ($one) {
                case 'voice_income':
                    $text = '语音收益(元)';
                    break;
                case 'video_income':
                    $text = '视频收益(元)';
                    break;
                case 'msg_income':
                    $text = '文字收益(元)';
                    break;
                case 'gift_income':
                    $text = '礼物收益(元)';
                    break;
                case 'invite_recharge_income':
                    $text = '邀请充值收益(元)';
                    break;
            }
            $this->return['earnings_list'][] = ['text' => $text, 'value' => $value];
        }
    }
    /**
     * 获取推广员成员数量
     * @return void
     */
    private function getMemberData()
    {
        $this->return['member_num'] = $this->guildHostModel->where('guild_manage_id', $this->manageInfo->id)->count() ?? 0;
    }
    /**
     * 重置返回数据
     * @return void
     */
    private function resetReturn()
    {
        $this->return = ['total_balance' => '0.00', 'have_access_balance' => '可提现余额：0.00', 'audit_access_balance' => '审核中金额：0.00', 'is_have_account' => 0, 'share_text' => '邀请女用户入驻，获取更多收益', 'share_url' => '', 'guild_name' => '', 'total_earnings' => '0.00', 'earnings_date' => '', 'earnings_list' => [], 'member_num' => 0, 'tips' => '余额每日五点更新,收益数据每三小时更新'];
    }
}